$( function () {

    $( ".box tr:last" ).find( "td" ).css( 'border-bottom', 'none' );

    $( ".step2" ).click( function () {
        var obj = $( this ).parents( 'form' );
        var jump = obj.attr( 'action' );
        obj.attr( 'action', '?a=checkDB' );
        ajaxPOST( $( this ), obj, function ( result ) {
            if ( result == 'success' ) {
                window.setTimeout( function () {
                    obj.attr( 'action', jump );
                    obj.submit();
                }, 1500 );
                return false;
            }
            if ( result == 'unique' ) {
                window.setTimeout( function () {
                    $.jBox.closeTip();
                    $.jBox.confirm( "<p>当前数据库当中已经含有同样表前缀的数据表;</p>" +
                        "<p>您可以修改“表前缀”来避免删除旧的数据;</p>" +
                        "<p><b>继续安装会清空所有记录。</b></p>", "安装提示", function ( v ) {
                        if ( v == true ) {
                            obj.attr( 'action', jump );
                            obj.submit();
                            return true;
                        }
                        return true;
                    }, { top: '30%', buttons: { '继续安装': true, '取消': false} });
                }, 1500 );
                return false;
            }
            window.setTimeout( function(){
                $.jBox.closeTip();
                $.jBox.tip( result );
            }, 2000 );
            return false;
        } );
        return false;
    });

    $( ".step3" ).click( function () {
        if ( !validate() ) {
            return false;
        }
        if ( !check_equal( 'user[pass]', 'user[pass1]' ) ) {
            $.jBox.tip( "2次密码输入不一样!" );
            return false;
        }
        $( "form" ).submit();
    } );
} );

function check_equal( a, b ) {
    var aa = $( 'input[name="' + a + '"]' ).val();
    var bb = $( 'input[name="' + b + '"]' ).val();
    aa = $.trim( aa );
    bb = $.trim( bb );
    return aa == bb;
}

function install( debug ) {
    var obj = $( ".button" );
    var form = obj.parents( 'form' );
    var url = form.attr('action');
    var data = form.serialize();
    obj.attr( "disabled", true );
    obj.fadeTo( "fast", 0.5 );
    var t = null;
    var i = 0;
    $.post( url, data, function ( result ) {
        var tmp = result.split( "<br />" );
        var len = tmp.length - 1;
        $( '#notice' ).fadeIn( 1000 );

        if ( result.indexOf( 'body' ) != -1 || result.indexOf( 'Fatal error' ) != -1 ) {
            $( "#notice" ).html( result );
            return;
        }
        t = window.setInterval( function () {
            i++;
            if ( i >= len ) {
                window.clearInterval( t );
                obj.val( '安装完成...' );
                $.jBox.tip( "安装完成! 正在进入首页...", "success", { opacity: 0.5 } );
                window.setTimeout( function () { window.location = weburl; }, 3500 );
            }
            if ( tmp[i] ) {
                $( "#notice" ).append( tmp[i] + "<br />" );
            }
        }, 300 );
    });
}
